package edu.pku.sei.transformation.tgg.action;

import org.eclipse.jface.action.Action;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

import edu.pku.sei.transformation.editor.model.AtomicTransformation;
import edu.pku.sei.transformation.editor.views.icons.IconsProvider;
import edu.pku.sei.transformation.tgg.adapter.TGGAdapter;
import edu.pku.sei.transformation.tgg.codegen.TGGGenerator;

public class GenTGGAction extends Action {
	private AtomicTransformation transformation = null;

	public GenTGGAction(Object selectedElement) {
		setText("generate tgg file");
		setImageDescriptor(IconsProvider
				.getImageDescriptor(IconsProvider.FILE_EXPORT_ICO));
		if (selectedElement instanceof AtomicTransformation) {
			this.transformation = (AtomicTransformation) selectedElement;
		}
	}
	
	@Override
	public boolean isEnabled() {
		if (transformation != null)
			return true;
		else
			return false;
	}
	
	@Override
	public void run() {
		Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
				.getShell();
		FileDialog dialog = new FileDialog(shell, SWT.SAVE);
		dialog.setFileName(transformation.getName() + ".tgg");
		dialog.setFilterExtensions(new String[] { "*.tgg" });
		String addr = dialog.open();
		if (addr == null)
			return;
		TGGGenerator generator = new TGGGenerator();
		generator.generate(new TGGAdapter(transformation), addr);
	}
}
